package cn.jx.asc.user.service;

import cn.jx.asc.user.mapper.*;
import cn.jx.asc.user.pojo.*;
import cn.jx.asc.user.vo.DataShow;
import cn.jx.asc.user.vo.nowDataShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 周国伟
 * @desc TODO
 * @date 2018/6/6 10:44
 * @describe
 */
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
   @Autowired
   private UserMapper userMapper;
   @Autowired
   private RecordMapper recordMapper;
   @Autowired
   private RegionMapper regionMapper;
   @Autowired
   private GroupMapper groupMapper;
   @Autowired
   private ValveMapper valveMapper;
   @Autowired
   private PlanMapper planMapper;
   @Autowired
   private SensorMapper sensorMapper;
   @Autowired
   private TimeMapper timeMapper;
   @Autowired
   private ControlMapper controlMapper;
   @Autowired
   private HumidityMapper humidityMapper;

    @Override
    public User findUserByname(String username) {

        User user = userMapper.selectUserByname(username);
        return user;
    }

    @Override
    public int addUser(User user) {

       int rows = userMapper.insertSelective(user);
        return rows;
    }

    @Override
    public int updateUser(User user) {
        int rows = userMapper.updateByPrimaryKeySelective(user);
        return rows;
    }



    @Override
    public List<Record> findData(DataShow dataShow) {


        List<Record> recordList = recordMapper.selectDataBydataShowHistory(dataShow);

        return recordList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/20 14:46
     * @describe 查找该用户下的所有区域
     */
    @Override
    public List<Region> selectAllByUserId(Integer userId) {
        List<Region> regionList = regionMapper.selectAllByUserId(userId);
        return  regionList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/20 21:43
     * @describe 根据用户id查找该用户管理区域内的所有组
    */
    @Override
    public List<Group> selectAllGroupByUserId(Integer userId) {
        List<Group> groupList = groupMapper.selectAllGroupByUserId(userId);
        return  groupList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/21 15:16
     * @describe 根据用户id查找该用户管理的所有阀门
    */
    @Override
    public List<Valve> selectAllValveByUserId(Integer userId) {
        List<Valve> valveList = valveMapper.selectAllValveByUserId(userId);
        return valveList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/21 15:16
     * @describe 添加一个区域
    */
    public int addRegion(Region region){
        int row = regionMapper.insertSelective(region);
        return row;
    }

   /**
    * @author LaiWeiXing
    * @date 2018/6/21 19:41
    * @describe 根据区域id删除该区域
   */
   public  int deleteRegionByRegionId(Integer regionId){
       int row = regionMapper.deleteByPrimaryKey(regionId);
       return row;
   }

   /**
    * @author LaiWeiXing
    * @date 2018/6/21 20:41
    * @describe 修改区域名
   */
   public int updateAreaName(Region region){
       int row = regionMapper.updateByPrimaryKeySelective(region);
       return row;
   }

   /**
    * @author LaiWeiXing
    * @date 2018/6/22 14:55
    * @describe 根据用户id查找该用户下的专家方案
   */
   public List<Plan> findPlanListByUserId(Integer userId){
       List<Plan> planList = planMapper.findPlanListByUserId(userId);
       return planList;
   }

    @Override
    public List<nowDataShow> findNowData() {
        List<nowDataShow> dataShowList = recordMapper.selectNowdataShow();
        return dataShowList;
    }

    @Override
    public List<Region> findRegionWithGroup() {

        List<Region> regionList = regionMapper.findRegionWithGroup();
        return regionList;
    }

    @Override
    public List<Valve> findValveData() {
        List<Valve> valveList = valveMapper.selectValveData();
        return valveList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/24 15:57
     * @describe 添加组
    */
    @Override
    public int insertGroupSelective(Group group) {
        int row = groupMapper.insertSelective(group);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/24 15:59
     * @describe 根据区域id查找到该区域对象
    */
    @Override
    public Region selectRegionByPrimaryKey(Integer regionId) {
        Region region = regionMapper.selectByPrimaryKey(regionId);
        return region;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/24 17:42
     * @describe 根据用户id查找一个User对象
    */
    @Override
    public User selectUserByPrimaryKey(Integer userId){
        User user = userMapper.selectByPrimaryKey(userId);
        return  user;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/24 19:54
     * @describe 添加组
    */
    @Override
    public int insertGroup(Group group) {
        int row = groupMapper.insertGroup(group);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/24 19:54
     * @describe 根据组id查找组对象
    */
    public Group findGroupByGroupId(Integer groupId){
        Group group = groupMapper.selectByPrimaryKey(groupId);
        return group;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 8:39
     * @describe 修改组
    */
    @Override
    public int updateGroup(Group group) {
       int row = groupMapper.updateByPrimaryKey(group);
       return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 14:45
     * @describe 删除组
    */
    @Override
    public int deleteGroup(Integer groupId){
        int row = groupMapper.deleteByPrimaryKey(groupId);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 14:45
     * @describe 添加传感器
    */
    @Override
    public int insertSensor(Sensor sensor){
        int row = sensorMapper.insertSelective(sensor);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 15:53
     * @describe 查找最新插入的组id
    */
    @Override
    public int findLastGroupId(){
        int groupId = groupMapper.findLastGroupId();
        return groupId;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 17:23
     * @describe 删除传感器
    */
    @Override
    public int deleteSensorByPrimaryKey(Integer sensorId){
        int row = sensorMapper.deleteByPrimaryKey(sensorId);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 20:38
     * @describe 根据区域id获取组集合
    */
    @Override
    public List<Group> findGroupListByRegionId(Integer regionId){
        List<Group> groupList = groupMapper.findGroupListByRegionId(regionId);
        return groupList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/25 21:27
     * @describe 插入节点
    */
    @Override
    public int addValve(Valve valve){
        int row = valveMapper.insertValve(valve);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/26 8:35
     * @describe 删除阀门
    */
    @Override
    public int deleteValveByValveId(Integer valveId){
        int row = valveMapper.deleteByPrimaryKey(valveId);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/26 21:09
     * @describe 根据用户id和作物种类查找专家方案
    */
    @Override
    public Plan findPlanByUserIdAndCropType(Integer userId ,String cropType){
        Plan plan = planMapper.findPlanByUserIdAndCropType(userId ,cropType);
        return plan;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/27 11:07
     * @describe 根据专家方案id和生长周期查找浇灌时间
    */
    @Override
    public List<Time> findTimeListByPlanIdAndCycleId(Integer planId ,Integer cycleId){
        List<Time> timeList = timeMapper.findTimeListByPlanIdAndCycleId(planId ,cycleId);
        return timeList;
    }

    @Override
    public List<Valve> findValveData(String groupId) {
        List<Valve> valveList = valveMapper.selectValveDataByGroupId(groupId);
        return valveList;
    }

    @Override
    public List<Group> findGroupAndValve() {
        List<Group> groupList = groupMapper.selectGroupAndValve();
        return groupList;
    }

    @Override
    public int updateValve(String valveId, String controlType, List<Group> groupList) {  //跟新在定时模式时的阀门控制方式

        int rows =0;

        if(valveId.equals("全部")){
            for(Group group:groupList){
                for(Valve valve1:group.getValveList()){
                    Valve valve = new Valve();
                    valve.setValveId(valve1.getValveId());
                    short controlTy = Short.parseShort(controlType);
                    valve.setControlType(controlTy);
                    int row_s = valveMapper.updateByPrimaryKeySelective(valve);
                    rows+=row_s;
                }
            }

        }else {
            Valve valve = new Valve();
            int valveID = Integer.parseInt(valveId);
            valve.setValveId(valveID);
            short controlTy = Short.parseShort(controlType);
            valve.setControlType(controlTy);
            rows = valveMapper.updateByPrimaryKeySelective(valve);
        }


        return rows;
    }

    @Override    //用户自己设计的浇灌方案
    public int addControl(List<String> listStartTime, List<String> listStopTime, List<String> listIntervalDay, String valveId, List<Group> groupList) {

        Control control = new Control();
        int rows = 0;
        if(valveId.equals("全部")){
            for(Group group:groupList){
                for(Valve valve1:group.getValveList()){
                    for(int i=0;i<5;i++){
                        if(listStartTime.get(i)==null||listStopTime.get(i)==null||listIntervalDay.get(i)==null){
                            continue;
                        }else {
                            control.setValveId(valve1.getValveId());
                            control.setStartTime(listStartTime.get(i));
                            control.setStopTime(listStopTime.get(i));
                            control.setIntervalDay(listIntervalDay.get(i));
                            int row_s = controlMapper.insertSelective(control);
                            rows+=row_s;
                        }
                    }
                }
            }
        }else {
            int valveID = Integer.parseInt(valveId);
            for(int i=0;i<5;i++){
                if(listStartTime.get(i)==null||listStopTime.get(i)==null||listIntervalDay.get(i)==null){
                    continue;
                }else {
                    control.setValveId(valveID);
                    control.setStartTime(listStartTime.get(i));
                    control.setStopTime(listStopTime.get(i));
                    control.setIntervalDay(listIntervalDay.get(i));
                    int row_s = controlMapper.insertSelective(control);
                    rows+=row_s;
                }
            }
        }
        return rows;
    }

    @Override           //阀门控制中的定量模式
    public int addHumidity(String humidity, String groupId, List<Region> regionList) {
        Humidity humidity1 = new Humidity();
        int rows = 0;
        if(groupId.equals("全部")){
            for(Region region:regionList){
                for(Group group:region.getGroupList()){
                    short humi = Short.parseShort(humidity);
                    humidity1.setHumidity(humi);
                    humidity1.setGroupId(group.getGroupId());
                    int row_s = humidityMapper.insertSelective(humidity1);
                    rows+=row_s;
                }
            }
        }else {
            short humi = Short.parseShort(humidity);
            int grouId = Integer.parseInt(groupId);
            humidity1.setHumidity(humi);
            humidity1.setGroupId(grouId);
            rows = humidityMapper.insertSelective(humidity1);
        }

        return rows;
    }

    @Override //跟新在定量模式时的阀门控制方式
    public int updateValve_ration(String groupId, String controlType, List<Region> regionList) {
        Valve valve = new Valve();
        int rows = 0;
        if(groupId.equals("全部")){
            for(Region region:regionList){
                for(Group group:region.getGroupList()){
                    valve.setGroupId(group.getGroupId());
                    short control = Short.parseShort(controlType);
                    valve.setControlType(control);
                    int row_s = valveMapper.updateByPrimaryKeySelectiveByGroupId(valve);
                    rows+=row_s;
                }
            }
        }else {
            int groupID = Integer.parseInt(groupId);
            valve.setGroupId(groupID);
            short control = Short.parseShort(controlType);
            valve.setControlType(control);
            rows = valveMapper.updateByPrimaryKeySelectiveByGroupId(valve);
        }
        return rows;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/28 10:49
     * @describe 插入定湿度
    */
    public int insertHumidity(Humidity humidity){
        int row = humidityMapper.insertSelective(humidity);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/28 11:03
     * @describe 定湿度控制，根据组id更改阀门状态为2
     */
    public int updateStateByGroupId(Integer groupId){
        int row = valveMapper.updateStateByGroupId(groupId);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/28 11:54
     * @describe 根据组id查找该组阀门
     */
    public List<Valve> findValveByGroupId(Integer groupId){
        List<Valve> valveList = valveMapper.findValveByGroupId(groupId);
        return  valveList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/28 16:58
     * @describe
    */
    public List<Time> findTimeListByPlanId(Integer planId){
        List<Time> timeList = timeMapper.findTimeListByPlanId(planId);
        return timeList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/28 20:55
     * @describe 根据用户id查找组集合
     */
    public List<Group> findGroupListByUserId(Integer groupId){
        List<Group> groupList = groupMapper.findGroupListByUserId(groupId);
        return  groupList;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/6/29 10:54
     * @describe 插入用户自定义控制
    */
    public int insertControl(Control control){
        int row = controlMapper.insertSelective(control);
        return row;
    }

    /**
     * @author ZhouGuoWei
     * @date 2018/6/29 11:06
     * @describe 更新阀门控制模式中的手动控制
     */
    @Override
    public int updateValveByVaList(List<String> valvaIdList, String controlType, List<String> buttonvalList) {
        int rows =0;

        Valve valve = new Valve();
        for(int j=0;j<valvaIdList.size();j++){
            if(valvaIdList.get(j)==null){
                continue;
            }else {
                String valID =String.valueOf(valvaIdList.get(j));
                int valId = Integer.parseInt(valID);
                valve.setValveId(valId);
                if(buttonvalList.get(j).equals("打开")){
                    Short ids = 1;
                    valve.setValveState(ids);
                }else {
                    Short ids = 0;
                    valve.setValveState(ids);
                }
                Short conTrol = Short.parseShort(controlType);
                valve.setControlType(conTrol);
                int row = valveMapper.updateByPrimaryKeySelective(valve);
                rows+=row;
            }
        }

        return rows;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/7/3 19:38
     * @describe 插入当前数据
    */
    public int insertRecord(Record record){
        int row = recordMapper.insertSelective(record);
        return row;
    }

    /**
     * @author LaiWeiXing
     * @date 2018/7/3 21:36
     * @describe 修改阀门
    */
    public int updateValve(Valve valve){
        int row = valveMapper.updateByPrimaryKeySelective(valve);
        return row;
    }

    /**
     * @author ZhouGuoWei
     * @date 2018/7/5 17:08
     * @describe 手机安卓历史数据查询
     */

    @Override
    public List<Record> findAndroidData(DataShow dataShow) {

        List<Record> recordList = recordMapper.selenctAdroidDataBydataShowHistory(dataShow);

        return recordList;
    }


}
